package com.my.test.basic;

public class ReverseList {
	
	private static class Node {
		public Object val;
		public Node next;
		
		public Node(Object val, Node n) {
			this.val = val;
			this.next = n;
		}
		public Node() {
			
		}
		
		public Object getVal() {
			return val;
		}
		public void setVal(Object val) {
			this.val = val;
		}
		public Node getNext() {
			return next;
		}
		public void setNext(Node next) {
			this.next = next;
		}
		
		public String toString() {
			if(next != null) {
				return val.toString() + "->" + next.val;
			} else {
				return val.toString() + "->" + "null";
			}
		}
		
		public String deepToString() {
			Node n = next;
			StringBuilder buf = new StringBuilder();
			buf.append(val);
			while(n != null) {
				buf.append("->");
				buf.append(n.val);
				n = n.next;
			}
			return buf.toString();
		}
	}
	
	public static void reverseList(Node n) {
//		Node tmp = n;
		Node cur = n;
		Node next = cur.getNext();
		Node tmpNext = next.next;
		next.next = cur;
		cur.next = null;
		cur = next;
		
		while(tmpNext != null) {
			next = tmpNext;
			tmpNext = next.next; 
			next.next = cur;
			cur = next;
		}
	}

	public static void main(String[] args) {
		Node n5 = new Node(5, null);
		Node n4 = new Node(4, n5);
		Node n3 = new Node(3, n4);
		Node n2 = new Node(2, n3);
		Node n1 = new Node(1, n2);
		System.out.println(n1.deepToString());
		reverseList(n1);
/*		System.out.println(n5);
		System.out.println(n4);
		System.out.println(n3);
		System.out.println(n2);
		System.out.println(n1);	*/	
		System.out.println(n5.deepToString());
	}

}
